#include <stdint.h>
#include "uart_printf.h"
#include <stdarg.h>
#include <stdio.h>
#include "usart.h"
#include <stdint.h>
#include "uart_printf.h"
#include <stdarg.h>
#include <stdio.h>
#include "usart.h"

/**************************普通的printf重定向**************************************************/

#ifdef UART_NORMAL

#ifdef __GNUC__
/* With GCC, small printf (option LD Linker-Libraries-Small printf
   set to Yes) calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */




PUTCHAR_PROTOTYPE {
    /* Place your implementation of fputc here */
    /* e.g. write a character to the USART1 and Loop until the end of transmission */
    // 注意下面第一个参数是&huart1，因为cubemx配置了串口1自动生成
    HAL_UART_Transmit(&huart3, (uint8_t *) &ch, 1, 0xFFFF);
    return ch;
}


int _write(int file, char *ptr, int len) {
    int DataIdx;
    for (DataIdx = 0; DataIdx < len; DataIdx++) { __io_putchar(*ptr++); }
    return len;
}




/*************************配置信息*******************/
DebugLevel currentDebugLevel = DEBUG_LEVEL_DEBUG;  // 默认设置为所有信息，可配置
/*************************配置信息*******************/

//日志分等级输出
void debug_printf(DebugLevel level, const char *format, ...)
{
    if (level <= currentDebugLevel)
    {
        va_list args;
        va_start(args, format);
        vprintf(format, args);
        va_end(args);
    }
}




/**************日志分等级输出扩展********************/

const char* debugLevelStrings[] = {
    "NONE",
    "ERROR",
    "WARNING",
    "INFO",
    "DEBUG",
    "VERBOSE"
};

void cout(DebugLevel level, const char *format, ...)
{
    if (level <= currentDebugLevel)
    {
        va_list args;
        va_start(args, format);
        // 打印调试级别前缀
        printf("[%s] ", debugLevelStrings[level]);
        vprintf(format, args);
        printf("\r\n");
        va_end(args);
    }
}
/**************日志分等级输出扩展********************/



void printf_test(void)
{
    debug_printf(DEBUG_LEVEL_ERROR, "This is an error message.\n");
    debug_printf(DEBUG_LEVEL_WARNING, "This is a warning message.\n");
    debug_printf(DEBUG_LEVEL_INFO, "This is an info message.\n");
    debug_printf(DEBUG_LEVEL_DEBUG, "This is a debug message.\n");
    debug_printf(DEBUG_LEVEL_VERBOSE, "This is a verbose message.\n");
}



#endif

/**************************普通的printf重定向**************************************************/

